<script lang="ts" setup>
  import dayjs from 'dayjs';
  import { onMounted, ref } from 'vue';
  import useTable from '@/hooks/table';
  import { CmsAuthor } from '@/types/cms';
  import { deleteAuthor, getAuthorList } from '@/api/cms';
  import { Modal } from '@arco-design/web-vue';
  import AuthorEditor from './editor.vue';

  const { pager, loading, total, list } = useTable<CmsAuthor>();

  const getData = async () => {
    loading.value = true;
    try {
      const res = await getAuthorList({
        ...pager.value,
      });
      total.value = res?.data?.data?.total || 0;
      list.value = res?.data?.data?.list || [];
    } finally {
      loading.value = false;
    }
  };

  const handlePageIndexChanged = (pageIndex: number) => {
    pager.value.pageIndex = pageIndex;
    getData();
  };

  const editorConfig = ref<{
    visible: boolean;
    author?: CmsAuthor;
  }>({
    visible: false,
  });

  const beginCreate = () => {
    editorConfig.value = { visible: true };
  };

  const beginEdit = (author: CmsAuthor) => {
    editorConfig.value = {
      visible: true,
      author,
    };
  };

  const handleEditorDone = (refresh: boolean) => {
    if (refresh) {
      getData();
    }
    editorConfig.value = {
      visible: false,
    };
  };

  const handleDelete = (author: CmsAuthor) => {
    Modal.confirm({
      title: '确认删除？',
      content: '删除后无法恢复',
      async onBeforeOk() {
        await deleteAuthor(author.id);
        getData();
      },
    });
  };

  onMounted(() => {
    getData();
  });
</script>

<template>
  <div style="padding: 20px">
    <a-row :gutter="[20, 20]">
      <a-col>
        <a-card title="作者管理" :bordered="false">
          <template #extra>
            <a-button type="primary" @click="beginCreate">
              <template #icon>
                <icon-plus />
              </template>
              添加作者
            </a-button>
          </template>
        </a-card>
      </a-col>

      <a-col>
        <a-card :bordered="false">
          <a-table
            :bordered="{ cell: true }"
            :data="list"
            :loading="loading"
            :pagination="{
              total,
              current: pager.pageIndex,
              pageSize: pager.pageSize,
              showTotal: true,
              showJumper: true,
            }"
            @page-change="handlePageIndexChanged"
          >
            <template #columns>
              <a-table-column title="作者名称" data-index="authorName">
              </a-table-column>
              <a-table-column title="添加时间" data-index="gmtCreate">
                <template #cell="{ record }">
                  {{ dayjs(record.gmtCreate).format('YYYY/MM/DD HH:mm:ss') }}
                </template>
              </a-table-column>
              <a-table-column title="修改时间">
                <template #cell="{ record }">
                  {{
                    record.gmtUpdate
                      ? dayjs(record.gmtUpdate).format('YYYY/MM/DD HH:mm:ss')
                      : '-'
                  }}
                </template>
              </a-table-column>
              <a-table-column title="操作" :width="180" align="center">
                <template #cell="{ record }">
                  <a-space>
                    <a-link @click="beginEdit(record)">编辑</a-link>
                    <a-link
                      style="color: rgb(var(--red-6))"
                      @click="handleDelete(record)"
                    >
                      删除
                    </a-link>
                  </a-space>
                </template>
              </a-table-column>
            </template>
          </a-table>
        </a-card>
      </a-col>
    </a-row>

    <AuthorEditor
      v-if="editorConfig.visible"
      :author="editorConfig.author"
      @close="handleEditorDone"
    />
  </div>
</template>
